﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using bs_ShowRoom;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace ShowRoom.admin.ctrJs
{
    public partial class PowerUser : System.Web.UI.Page
    {
        private const string strNoData = "No Data Found!";

        private const string updMsg = "Delete complete!";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user_id"] == null || Session["userId"] == "")
            {
                Response.Redirect("Login.aspx");
            }
            string strEventName = Convert.ToString(Request.Params["EventName"]);
            switch (strEventName)
            {
                case "getDetail":
                    {
                        string user_id = "";
                        string customer_id = "";
                        if (Request["user_id"] != null)
                            user_id = Request["user_id"];
                        GetData(user_id);
                    }
                    break;
            }
        }
        private void GetData(string user_id)
        {

            System.Text.StringBuilder sbErrMsg = new System.Text.StringBuilder("");
            System.Text.StringBuilder sbServerMsg = new System.Text.StringBuilder("");

            try
            {
                DataTable dt = GetInfouser(user_id, "3");

                gvpowerUser.DataSource = dt;
                int count = dt.Rows.Count;
                if (count == 0)
                {
                    dt.Rows.Add(dt.NewRow());
                }
                gvpowerUser.DataBind();

                //GridViewRow header = gvFact_detail.HeaderRow;
                //HtmlImage imgAdd = (HtmlImage)header.FindControl("imgAdd");              
                //imgAdd.Attributes["onclick"] = "OpenPickFactToMenu(this,'" + menu_id + "','" + menu + "','" + decen + "')";
                if (count == 0)
                {
                    int columns = gvpowerUser.Columns.Count;
                    gvpowerUser.Rows[0].Cells[0].Text = strNoData;
                    gvpowerUser.Rows[0].Cells[0].ColumnSpan = columns;
                    for (int i = 1; i < columns; i++)
                    {
                        gvpowerUser.Rows[0].Cells.RemoveAt(1);
                    }
                }


            }
            catch (Exception ex)
            {
                sbErrMsg.Append("Err!");
                sbErrMsg.Append(ex.Message);
            }
            if (sbErrMsg.ToString().Trim() == "") //OK
            {
               
                System.IO.StringWriter sw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);


                HtmlForm hf = new HtmlForm();
                Controls.Add(hf);
                hf.Controls.Add(litSpliter1);
                hf.Controls.Add(gvpowerUser);
                hf.Controls.Add(litSpliter2);
                hf.RenderControl(htw);
                string strBody = sw.ToString();
                strBody = Regex.Split(strBody, "<#BreakChar#>", RegexOptions.IgnoreCase)[1];
                strBody = JSStringFormat(strBody);

                sbServerMsg.Append("{");
                sbServerMsg.Append("IsOK:true");
                sbServerMsg.Append(",ServerMsg:'Update complete!'");
                sbServerMsg.Append(",Result:'" + strBody + "'");
                sbServerMsg.Append("}");
            }
            else //execute faile
            {
                sbServerMsg.Append("{");
                sbServerMsg.Append("IsOK:false");
                sbServerMsg.Append(",ServerMsg:'" + JSStringFormat(sbErrMsg.ToString().Trim()) + "'");
                sbServerMsg.Append("}");
            }
            Thread.Sleep(100);
            Response.Clear();
            Response.Write(sbServerMsg.ToString());
            Response.End();

        }
        private string JSStringFormat(string s)
        {
            return s.Replace("\r", "\\r").Replace("\n", "\\n").Replace("'", "\\'").Replace("\"", "\\\"");
        }
        private DataTable GetInfouser(string user_id, string Type)
        {
            bs_menu mybs = new bs_menu(ConfigurationManager.ConnectionStrings["ShowRoomConnectionString"].ToString());
            PccMsg myMsg = new PccMsg();
            myMsg.CreateFirstNode("user_id", user_id);
            myMsg.CreateFirstNode("Type", Type);
            DataSet ds = mybs.DoReturnDataSet("GETPOWESUSER", myMsg.GetXmlStr, "");

            DataTable dt = new DataTable();
            if (ds.Tables.Count > 0)
                dt = ds.Tables[0];

            return dt;

        }


        protected void gvpowerUser_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                //check quyền dc xem menu
                string chk_view = drv["chk_view"].ToString().Trim();
                if (chk_view == "Y")
                    ((CheckBox)e.Row.FindControl("chkView")).Checked = true;
                //check quyền dc thêm mới
                string chk_add = drv["chk_add"].ToString().Trim();
                if (chk_add == "Y")
                    ((CheckBox)e.Row.FindControl("chkAdd")).Checked = true;
                //check quyền dc Chỉnh sữa
                string chkEdit = drv["chk_edit"].ToString().Trim();
                if (chkEdit == "Y")
                    ((CheckBox)e.Row.FindControl("chkEdit")).Checked = true;
                //check quyền dc xóa
                string chkDel = drv["chk_del"].ToString().Trim();
                if (chkDel == "Y")
                    ((CheckBox)e.Row.FindControl("chkDel")).Checked = true;

            }
        }
        private void btnSelectAdd_Click(object sender, System.EventArgs e)
        {
            for (int i = 0; i < gvpowerUser.Rows.Count; i++)
            {
                CheckBox chk_View = (CheckBox)gvpowerUser.Rows[i].FindControl("chkView");
                CheckBox chk_Add = (CheckBox)gvpowerUser.Rows[i].FindControl("chkAdd");
                CheckBox chk_Edit = (CheckBox)gvpowerUser.Rows[i].FindControl("chkEdit");
                CheckBox chk_Del = (CheckBox)gvpowerUser.Rows[i].FindControl("chkDel");

                string chkView = "";
                if (chk_View.Checked)
                    chkView = "Y";

                string chkAdd = "";
                if (chk_Add.Checked)
                    chkAdd = "Y";

                string chkEdit = "";
                if (chk_Edit.Checked)
                    chkEdit = "Y";

                string chkDel = "";
                if (chk_Edit.Checked)
                    chkDel = "Y";
                string mn_id = ((Label)gvpowerUser.Rows[i].FindControl("lblmn_id")).Text.ToString().Trim();
                string user_id = Request.QueryString["user_id"].ToString().Trim();
                if (chkView != "" || chkAdd != "" || chkEdit != "" || chkDel != "")
                {

                    string strReturn = fnInsUpdPowerUser("0", user_id, mn_id, chkView, chkAdd, chkEdit, chkDel, "0");
                    /*
                     string strReturn = hms_Per2.TrsPersonel(msghms2.GetXmlStr);
                     if (strReturn == "")
                     {
                         PccMsg msghms3 = new PccMsg();
                         msghms3.CreateFirstNode("RO_ID", RO_ID);
                         hms_Room hms_Per3 = new hms_Room();

                         DataTable dt = hms_Per3.GetRoomData(msghms3.GetXmlStr);
                         string BM_ID = dt.Rows[0]["BM_ID"].ToString().Trim();
                         string BF_ID = dt.Rows[0]["BF_ID"].ToString().Trim();

                         string url = "Room.aspx?BM_ID=" + Server.UrlEncode(BM_ID) + "&BF_ID=" + Server.UrlEncode(BF_ID);
                         //Response.Redirect(url);
                         ScriptFunction.ShowMsgTranUrl_ScriptManager(updMsg, Page, url);
                     }
                     else
                     {
                         ScriptFunction.ShowMsg_ScriptManager(strReturn, Page);
                     }
                     */
                }
            }
        }
        private string fnInsUpdPowerUser(string mn_id_d, string user_id, string mn_id, string chk_view, string chk_add, string chk_edit, string chk_del, string myType)
        {
            PccMsg myMsg = new PccMsg();
            myMsg.CreateFirstNode("mn_id_d", mn_id_d);
            myMsg.CreateFirstNode("user_id", user_id);
            myMsg.CreateFirstNode("mn_id", mn_id);
            myMsg.CreateFirstNode("chk_view", chk_view);
            myMsg.CreateFirstNode("chk_add", chk_add);
            myMsg.CreateFirstNode("chk_edit", chk_edit);
            myMsg.CreateFirstNode("chk_del", chk_del);
            myMsg.CreateFirstNode("Type", myType);

            bs_menu mybs = new bs_menu(ConfigurationManager.ConnectionStrings["ShowRoomConnectionString"].ToString());

            string strXML = myMsg.GetXmlStr;
            string strReturn = mybs.DoReturnStr("INSUPDPOWERUSER", strXML, "");
            myMsg.LoadXml(strReturn);
            strReturn = myMsg.Query("Err").Trim();
            return strReturn;
            //*****************************************************************************
        }
        protected void gvpowerUser_RowCreated(object sender, GridViewRowEventArgs e)
        {
            string body = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=big5'>";
            body += "<style type='text/css'>.a00 {color:#AA0000}";
            body += ".cssButton{color: #1600e5;border: 1px solid #065790;filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#FFFFFF', EndColorStr='#D3DDFE');FONT-SIZE: 9pt;	padding:0 0 0 2pt;font-weight:lighter;font-family:verdana;height:22px;cursor: hand;}";
            body += "</style></head>";
            GridView gv = new GridView();
            /*
            if (e.Row.RowType == DataControlRowType.Header)
            {
                GridViewRow header = gv.HeaderRow;
                Button btnSelectAdd = new Button();
                btnSelectAdd.Text = "Lưu lại";
                btnSelectAdd.Width = 70;
                btnSelectAdd.CssClass = "cssButton";
                btnSelectAdd.Click += new System.EventHandler(btnSelectAdd_Click);
                e.Row.Cells[0].Controls.Add(btnSelectAdd);
            }
             */
        }
    }
}